const typeColorsArrList = {
  '水温℃': '#FF6347', // 珊瑚色
  'pH': '#00CED1', // 蓝绿色
  '溶解氧mg/L': '#FFD700', // 金色
  '亚硝酸盐氮mg/L': '#7CFC00', // 闪光绿
  '硝酸盐氮mg/L': '#20B2AA', // 海洋绿
  '氨氮mg/L': '#9370DB', // 紫色
  '石油类mg/L': '#FF69B4', // 粉红色
  '无机氮mg/L': '#00BFFF', // 深天蓝
  '粪大肠菌群个/L': '#FFA07A', // 海贝色
  '活性磷酸盐mg/L': '#32CD32', // 酸橙绿
  '化学需氧量mg/L': '#40E0D0', // 宝石绿
  '盐度‰': '#2730da', // 橙红色
  '叶绿素μg/L': '#6A5ACD', // 丁香紫
  '水质类别': '#FF8C00', // 橙色
  '富养化指数': '#48D1CC' // 中绿宝石
}

/**
 * 拼装多纵坐标折线图
 * @param dataList
 * @param locationType
 * @param padding
 * @returns {*[]}
 * @author 赵京涛
 */
export const useLinesChartsData = (dataList, locationType, padding) => {
  console.log('dataList', dataList)
  const data = []
  for (let i = 0; i < dataList.length; i++) {
    const position = (i % 2 === 0) ? 'left' : 'right'
    const index = Math.floor(i / 2)
    const currentOffset = position === 'left' ? index * locationType : index * locationType
    console.log('dataList[i].name',dataList[i].name)
    const obj = {
      nameLocation: 'middle',
      boundaryGap: [0, '50%'],
      max: dataList[i].name === '水质类别' ? 5 : null,
      axisLine: {
        show: true
      },
      // 调整名称的位置
      nameTextStyle: {
        padding: padding
      },
      axisTick: {
        show: true,
        inside: 'false',
        length: 10
      },
      type: 'value',
      name: dataList[i].name,
      position: position,
      offset: currentOffset,
      axisLabel: {
        formatter: function(value) {
          if(dataList[i].name !== '水质类别') {
            return value
          } else {
            const target = {
              1: 'Ⅰ类',
              2: 'Ⅱ类',
              3: 'Ⅲ类',
              4: 'Ⅳ类',
              5: '劣Ⅳ类',
            }
            return target[Math.round(value)]
          }
        }
      },
      splitLine: {
        show: true
      }
    }
    data.push(obj)
  }
  return data
}

/**
 * 多柱状图数据
 * @param dataList
 * @returns {*[]}
 * @author 赵京涛
 */
export const useLinesSeriesData = (dataList) => {
  const data = []
  let obj = {}
  for (let i = 0; i < dataList.length; i++) {
    if (dataList[i].name === '水质类别') {
      const listArr = dataList[i].list.map(k => k.value)
      const _menuObj = {
        '一类': 1,
        '二类': 2,
        '三类': 3,
        '四类': 4,
        '劣四类': 5,
        '': ''
      }
      const targetArr = listArr.map(target => _menuObj[target])
      obj = {
        name: dataList[i].name,
        type: 'line',
        data: targetArr,
        yAxisIndex: i,
        lineStyle: {
          color: typeColorsArrList[dataList[i].name]
        }
      }
    } else {
      obj = {
        name: dataList[i].name,
        type: 'line',
        data: dataList[i].list.map(k => k.value),
        yAxisIndex: i,
        lineStyle: {
          color: typeColorsArrList[dataList[i].name]
        }
      }
    }
    data.push(obj)
  }
  return data
}

/**
 * 重组单纵坐标折线图数据
 * @param data
 * @param fileName
 * @returns {*|*[]}
 * @author 赵京涛
 */
export const drawChartsDataHandler = (data, fileName) => {
  let newArr = []
  newArr = data.list.map(item => {
    const resItemList = item.list.map(listItem => {
      return {
        name: listItem.name,
        list: listItem.list.find(k => k.name === fileName)
      }
    })
    return {
      name: item.name,
      list: resItemList
    }
  })
  return newArr
}

/**
 * 按月组装数据
 * @param dataList
 * @param typeName
 * @author 赵京涛
 */
export const currentSeriesDataMoonFun = (dataList, typeName) => {
  const _menuObj = {
    '一类': '一类',
    '二类': '二类',
    '三类': '三类',
    '四类': '四类',
    '劣四类': '劣四类',
    '': ''
  }
  if (Array.isArray(dataList) && dataList.length > 0) {
    if (typeName === '水质类别') {
      return dataList.map(item => {
        return {
          name: item.name,
          type: 'line',
          data: item.list.map(k => k.list.value).map(target => _menuObj[target])
        }
      })
    } else {
      return dataList.map(item => {
        return {
          name: item.name,
          type: 'line',
          data: item?.list?.map(k => k?.list?.value)
        }
      })
    }
  }
}

/**
 * 多柱状图数据重组
 * @param xData
 * @param dataList
 * @param fileName
 * @author 赵京涛
 */
export const currentBarSeriesDataMoonFun = (xData, dataList, fileName) => {
  const _menuObj = {
    '一类': 1,
    '二类': 2,
    '三类': 3,
    '四类': 4,
    '劣四类': 5,
    '': ''
  }
  let newArr = []
  if (fileName !== '水质类别') {
    newArr = xData.map((item, index) => {
      return {
        name: item,
        type: 'bar',
        data: extractData(item, dataList, fileName),
        barWidth: 7,
        itemStyle: {
          color: index === 0 ? '#3ecccc' : index === 1 ? '#9044da' : '#e14585',
          barBorderRadius: [30, 30, 0, 0]
        }
      }
    })
  } else {
    newArr = xData.map((item, index) => {
      return {
        name: item,
        type: 'bar',
        data: extractData(item, dataList, fileName).map(target => _menuObj[target]),
        barWidth: 7,
        itemStyle: {
          color: index === 0 ? '#3ecccc' : index === 1 ? '#9044da' : '#e14585',
          barBorderRadius: [30, 30, 0, 0]
        }
      }
    })
  }
  return newArr
}
/**
 * @param keys
 * @param data
 * @param targetName
 * @returns {*[]}
 * @author 赵京涛
 */
const extractData = (keys, data, targetName) => {
  const result = []
  data.forEach(item => {
    const foundItem = findItemByName(item, targetName)
    if (foundItem) {
      const value = foundItem.list.find(subItem => subItem.name === keys).value
      result.push(value)
    }
  })
  return result
}
/**
 * 递归查询
 * @param data
 * @param name
 * @returns {*|null}
 * @author 赵京涛
 */
const findItemByName = (data, name) => {
  if (data.name === name) {
    return data
  } else if (data.list) {
    for (let i = 0; i < data.list.length; i++) {
      // 递归查找
      const foundItem = findItemByName(data.list[i], name)
      if (foundItem) {
        return foundItem
      }
    }
  }
  return null
}
